#pragma once

#include <GameLogic/BoardLogicIOBase.h>
#include "BuildingController.h"
#include <GameLogic/House.h>
#include "DxBoardDisplay.h"
#include "GuiController.h"

namespace catan
{
	class IGuiController;
	class UIController : public IBoardLogicIO, public IHelperBase, public IPlayerItemBuildHandler, public BoardDisplayBase, public IGuiCallback
	{
	public:
		UIController(void);
		~UIController(void);
		void ActiveWindow()
		{
			HWND hwnd = GetActiveWindow();
			if (hwnd)
			{
				BringWindowToTop(hwnd);
			}
		}

		virtual void OnCEvent(ProtocolBase& protocol)
		{
			switch(protocol.Type())
			{
				CASE_OnCEvent(SetGameStart);
				CASE_OnCEvent(BuildHouseApply);
				CASE_OnCEvent(BuildSettlementApply);
				CASE_OnCEvent(BuildRoadApply);
				CASE_OnCEvent(BuyCardApply);
				CASE_OnCEvent(EndTurnApply);
				CASE_OnCEvent(DieApply);
				CASE_OnCEvent(SetPlayerTrun);
				CASE_OnCEvent(PlayersHarvestApply);
				CASE_OnCEvent(SetBuildHouse);
				CASE_OnCEvent(SetBuildRoad);
				CASE_OnCEvent(SetBuildSettlement);
			}
		}

		virtual void Display(const Handle<Board> board);


		void Init();
///BuildSettHouse
		virtual void OnCEvent(SetBuildHouse& set)
		{
			VALID(mPlayerItemBuildController == 0);
			mPlayerItemBuildController = PlayerItemBuildController::CreateBuildHouse(this);
			mPlayerItemBuildController->Do(this);
		}

		virtual void OnBuildHouse(Handle<BuildPoint> buildPoint)
		{
			SafeDrop(mPlayerItemBuildController);
			BuildHouseReq req;
			req.mColor = mColor;
			req.mId = buildPoint.Id();
			mServer->OnSEvent(req);
		}

		virtual void OnCEvent(BuildHouseApply& apply)
		{
			Handle<BuildPoint> buildPoint = mHelper->GetBoard()->GetHandle<BuildPoint>(apply.mId);
			mHelper->GetDxBoardDisplay()->AddHouse(buildPoint->mBuilding.DynamicCast<House>());
		}
///BuildSettlement
		virtual void OnCEvent(SetBuildSettlement& set)
		{
			VALID(mPlayerItemBuildController == 0);
			mPlayerItemBuildController = PlayerItemBuildController::CreateBuildSettlemnet(this);
			mPlayerItemBuildController->Do(this);
		}

		virtual void OnBuildSelement(Handle<BuildPoint> buildPoint)
		{
			SafeDrop(mPlayerItemBuildController);
			BuildSettlementReq req;
			req.mColor = mColor;
			req.mId = buildPoint.Id();
			mServer->OnSEvent(req);
		}

		virtual void OnCEvent(BuildSettlementApply& apply)
		{
			Handle<BuildPoint> buildPoint = mHelper->GetBoard()->GetHandle<BuildPoint>(apply.mId);
			mHelper->GetDxBoardDisplay()->AddSettlement(buildPoint->mBuilding.DynamicCast<Settlement>());
		}
///BuildRaod
		virtual void OnCEvent(SetBuildRoad& set)
		{
			VALID(mPlayerItemBuildController == 0);
			mPlayerItemBuildController = PlayerItemBuildController::CreateBuildRoad(this);
			mPlayerItemBuildController->Do(this);
		}

		virtual void OnBuildRoad(Handle<Edge> edge)
		{
			SafeDrop(mPlayerItemBuildController);
			BuildRoadReq req;
			req.mColor = mColor;
			req.mId = edge.Id();
			mServer->OnSEvent(req);
		}

		virtual void OnCEvent(BuildRoadApply& apply)
		{
			Handle<Edge> edge = mHelper->GetBoard()->GetHandle<Edge>(apply.mId);
			mHelper->GetDxBoardDisplay()->AddRoad(edge->mRoad);
		}

		//
		virtual void OnBuildRoad(Handle<BuildPoint> buildPoint)
		{
			SafeDrop(mPlayerItemBuildController);
			BuildHouseReq req;
			req.mColor = mColor;
			req.mId = buildPoint.Id();
			mServer->OnSEvent(req);
		}

		virtual void OnCEvent(SetGameStart& set);

		virtual void OnCEvent(BuyCardApply&)
		{

		}

		virtual void OnCEvent(EndTurnApply&)
		{

		}

		virtual void OnCEvent(DieApply& apply)
		{
			mIGuiController->ShowDie(apply.mDieNumber);
		}

		virtual void OnCEvent(PlayersHarvestApply& apply)
		{
			mIGuiController->UpdateResourceCard();
		}


		virtual void OnCEvent(SetPlayerTrun&)
		{
			mIGuiController->SetCallback(this);
			mIGuiController->SetPlayerTrun();
		}

		virtual void OnDieReq()
		{
			DieReq req;
			req.mColor = mColor;
			mServer->OnSEvent(req);
		}


		PlayerItemBuildController* mPlayerItemBuildController;
		IGuiController* mIGuiController;
	};


}